###############################################################################
#
# Copyright (c) 2011 Rainmaker Entertainment
# All Rights Reserved.
#
# This file contains unpublished confidential and proprietary
# information of Rainmaker Entertainment.  The contents of this file
# may not be copied or duplicated, in whole or in part, by any
# means, electronic or hardcopy, without the express prior
# written permission of Rainmaker Entertainment.
#
#    $HeadURL: http://pt/src/trunk/maya/rnkAssetChecks/common.py $
#    $Revision: 67839 $
#    $Author: nkoubi $
#    $Date: 2013-09-17 10:42:36 -0700 (Tue, 17 Sep 2013) $
#
###############################################################################

###############################################################################
## INFOS
###############################################################################

# Common asset checks


###############################################################################
## MODULES
###############################################################################

import traceback
import re

try :
    import maya.cmds as cmds
    import maya.OpenMaya as OpenMaya
except :
    traceback.print_exc()
    print "Can't load Maya modules."
    
    
import rnkAssetChecks.core as rnkAssetChecksCore
reload(rnkAssetChecksCore)
        


###############################################################################
## FUNCTIONS
###############################################################################  
def checkForNonUniqueNames(assetRoot):
    """ 
    @brief Check to ensure that all transform names are unique.
    @return : List
    
    @param assetRoot - String - Asset root node.
    """

    badNodes = []

    ## Result Vars
    isUnique = True
    results = {}
    
    ## Check if there is no node who have the same name then the assetroot
    assetrootNonUnique = cmds.ls(assetRoot, long=True)
    if len(assetrootNonUnique) > 1 :
        return assetrootNonUnique
        

    ## Get a list of every DAG nodes under the asset root
    nodes = []
    if assetRoot :
        nodes = [node for node in rnkAssetChecksCore.walkDag(assetRoot)]
        
    ## If there is no asset root node, get every DAG in the current scene
    else :
        nodes = cmds.ls(dag=True, long=True)

    if not len(nodes) == 0 :
        for node in nodes :
#            if rnkAssetChecksCore.isTransform(node) is True :
            
            ## Get both node short name and full path name
            splitNode = node.split('|')
            shortName = splitNode[len(splitNode)-1]
            longName = node

            ## If the short name don't exist, add it and the long name to the results dict
            if results.has_key(shortName) is False :
                results[shortName] = set()
                results[shortName].add(longName)
            else:
                ## If it exist, check if the long name is already in the short name key
                match = False
                for existing in results[shortName]:
                    if rnkAssetChecksCore.asMObject(longName) == rnkAssetChecksCore.asMObject(existing) :
                        match = True
                        break
    
                ## If not, add the long name to the short name key
                if match is False :
                    isUnique = False
                    results[shortName].add(longName)


    ## If there is non-unique names, added them to the badNodes list
    if isUnique is False :
        for k, v in results.iteritems():
            if len(v) > 1:
                badNodes.extend([x for x in v])
                
                
    return badNodes





def checkIdNodeScale(assetRoot):
    """ 
    @brief Check to ensure that the root node's scale (sx,sy,sz) is (1,1,1).
    @return : Bool
    
    @param assetRoot - String - Asset root node.
    """

    values = cmds.getAttr('%s.scale' % assetRoot)
    if values != [(1.0, 1.0, 1.0)] :
        return False
    else:
        return True
    
    
def checkIdNodeTranslation(assetRoot):
    """ 
    @brief Check to ensure that the root node's translation (tx,ty,tz) is (0,0,0).
    @return : Bool
    
    @param assetRoot - String - Asset root node.
    """

    values = cmds.getAttr('%s.translate' % assetRoot)
    if values != [(0.0, 0.0, 0.0)] :
        return False
    else:
        return True

    
def checkIdNodeRotation(assetRoot):
    """ 
    @brief Check to ensure that the root node's rotation (rx,ry,rz) is (0,0,0).
    @return : Bool
    
    @param assetRoot - String - Asset root node.
    """

    values = cmds.getAttr('%s.rotate' % assetRoot)
    if values != [(0.0, 0.0, 0.0)] :
        return False
    else:
        return True


def checkNamespace(assetRoot):
    """ 
    @brief Remove from non-referenced (local/imported) nodes any namespaces.
    @return : Bool
    
    @param assetRoot - String - Asset root node.
    """
    
    try:
        rnkAssetChecksCore.deleteNamespaces()
        return True
    except:
        traceback.print_exc()
        return False